package configuration

import (
	"encoding/json"
	"fmt"
	"kop/modules/configuration/client"
	"kop/util/log"
)

//
type (
	DispatchConf struct {
		ID         int32   // 派遣ID
		Quality    int32   // 派遣品质
		Weight     int32   // 权重
		Time       int32   // 派遣时间（分钟）
		ShipID     int32   // 特定船只
		CostRate   float64 // 派遣消耗
		ItemID     int32   // 派遣奖励物品ID
		ItemNumber int32   // 派遣奖励物品固定数量
		ItemRate   int32   // 派遣奖励物品数量比例
	}

	DispatchCostConf struct {
		Peerage  int32 // 爵位等级
		CostCoin int64 // 每次派遣银币消耗
		Reward1  int32 // 派遣奖励1金币数量
		Reward2  int32 // 派遣奖励2魔戒碎片数量
		Reward3  int32 // 派遣奖励3强化石数量
	}
)

func (c DispatchConf) GetWeight() int32 {
	return c.Weight
}

func (c DispatchConf) GetID() int32 {
	return c.ID
}

var (
	DispatchConfigs     []DispatchConf
	DispatchCostConfigs = map[int32]DispatchCostConf{}
)

func init() {

	var err error
	if err = json.Unmarshal(client.Load("DispatchTemplate"), &DispatchConfigs); err != nil {
		panic(err)
	}

	log.Info("Load DispatchTemplate.json success.")

	//********************************************************************//

	if err = json.Unmarshal(client.Load("DispatchCost"), &DispatchCostConfigs); err != nil {
		panic(err)
	}

	log.Info("Load DispatchCost.json success.")
}

func GetDispatch(ID int32) DispatchConf {

	for _, v := range DispatchConfigs {
		if v.ID == ID {
			return v
		}
	}

	panic(fmt.Errorf("DispatchConf [%d] not found", ID))
}

func GetDispatchCostByPeerage(peerage int32) DispatchCostConf {

	if v, ok := DispatchCostConfigs[peerage]; !ok {
		panic(fmt.Errorf("DispatchCostConf [%d] not found", peerage))
	} else {
		return v
	}
}
